//
//  NDPersonalEnumView.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/14.
//

import SwiftUI

struct NDPersonalEnumView: View {
    let title: String
    let dataSource: [Courseatic]
    var onSelectionChanged: ((Int, Courseatic?) -> Void)?
    var onConfirmAction: ((Courseatic?) -> Void)?
    var onCloseAction: (() -> Void)?
    
    @State private var selectedID: UUID?
    
    var body: some View {
        VStack(spacing: 0) {
            
            Spacer()
                .frame(height: 25)
            
            ZStack(alignment: .trailing) {
                titleView
                closeButton
            }

            ScrollView {
                LazyVStack(spacing: 10) {
                    ForEach(dataSource) { item in
                        rowView(for: item)
                    }
                }
            }
            .padding(.top, 15)
            
            Spacer()
            
            confirmButton
        }
    }
    
    private var titleView: some View {
        Text(title)
            .font(.system(size: 18, weight: .medium))
            .frame(maxWidth: .infinity, alignment: .center)
            .padding(.bottom, 16)
    }
    
    private func rowView(for item: Courseatic) -> some View {
        HStack {
            Spacer()
            Text(item.interestacle ?? "unKnown")
                .frame(maxWidth: .infinity, alignment: .leading)
                .frame(height: 50)
                .font(.system(size: 16, weight: .semibold))
                .foregroundColor(item.id == selectedID ? Color(hex: "#FFFFFF") : Color(hex: "#2D4173"))
                .padding(.horizontal, 16)
                .padding(.vertical, 5)
                .background(
                    RoundedRectangle(cornerRadius: 8)
                        .fill(item.id == selectedID ? Color(hex: "#FD632B") : Color(hex: "#FFF6F3"))
                )
                .contentShape(Rectangle())
            
            Spacer()
        }
        .padding(.horizontal, 16)
        .onTapGesture {
            selectedID = item.id
            let index = dataSource.firstIndex { $0.id == selectedID } ?? -1
            onSelectionChanged?(index, item)
        }
    }
    
    private var confirmButton: some View {
        Button(action: {
            let selectedItem = dataSource.first { $0.id == selectedID }
            onConfirmAction?(selectedItem)
        }) {
            Text(localizedString("Confirm"))
                .frame(maxWidth: .infinity)
                .font(.system(size: 18, weight: .bold))
                .foregroundColor(Color(hex: "#FFFFFF"))
                .padding(.horizontal, 15)
                .padding(.vertical, 10)
                .frame(height: 48)
                .background(Color(hex: "#271F24"))
                .clipShape(Capsule())
                .contentShape(Rectangle())
        }
        .padding(.horizontal, 16)
        .padding(.bottom, 16)
    }
    
    private var closeButton: some View {
        Button(action: {
            onCloseAction?()
        }) {
            Image(systemName: "xmark")
                .font(.system(size: 16))
                .frame(width: 20, height: 20)
                .foregroundColor(Color(hex: "#271F24"))
        }
        .padding(.top, -7)
        .padding(.trailing, 20)
    }
}

